﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace TPRenderLibrary
{
    public class SpriteDef
    {
        int frameWidth;
        int frameHeight;
        int xCount;
        protected Vector2 center;
        /// <summary>
        /// All frames in the animation arranged horizontally.
        /// </summary>
        public Texture2D Texture
        {
            get { return texture; }
        }
        Texture2D texture;

        /// <summary>
        /// Duration of time to show each frame.
        /// </summary>
        public float FrameTime
        {
            get { return frameTime; }
        }
        float frameTime;

        /// <summary>
        /// When the end of the animation is reached, should it
        /// continue playing from the beginning?
        /// </summary>
        public bool IsLooping
        {
            get { return isLooping; }
        }
        bool isLooping;

        /// <summary>
        /// Gets the number of frames in the animation.
        /// </summary>
        public int FrameCount
        {
            get { return Texture.Width / FrameWidth; }
        }

        /// <summary>
        /// Gets the width of a frame in the animation.
        /// </summary>
        public int FrameWidth
        {
            // Assume square frames.
            get { return Texture.Height; }
        }

        /// <summary>
        /// Gets the height of a frame in the animation.
        /// </summary>
        public int FrameHeight
        {
            get { return Texture.Height; }
        }

        /// <summary>
        /// Constructors a new animation.
        /// </summary>        
        public SpriteDef(ContentManager content, string resource, float frameTime, bool isLooping, int frameWidth, int frameHeight, int xCount, Vector2 center)
        {
            this.texture = content.Load<Texture2D>(resource);
            this.frameTime = frameTime;
            this.isLooping = isLooping;
            this.frameWidth = frameWidth;
            this.frameHeight = frameHeight;
            this.xCount = xCount;
            this.center = center;
        }
        public SpriteDef(Texture2D texture, float frameTime, bool isLooping, int frameWidth, int frameHeight, int xCount, Vector2 center)
        {
            this.texture = texture;
            this.frameTime = frameTime;
            this.isLooping = isLooping;
            this.frameWidth = frameWidth;
            this.frameHeight = frameHeight;
            this.xCount = xCount;
            this.center = center;
        }
        public virtual void Draw(GameWindow window, GameTime gameTime, SpriteBatch spriteBatch, int img, Vector2 pos, float fAngle, Color color)
        {
            Rectangle rtSpriteD = new Rectangle((int)pos.X, (int)pos.Y, frameWidth, frameHeight);
            int x = img % xCount;
            int y = img / xCount;
            Rectangle rtSpriteSrc = new Rectangle(x * frameWidth, y * frameHeight, frameWidth, frameHeight);
            spriteBatch.Draw(Texture, rtSpriteD, rtSpriteSrc, color, fAngle, center, SpriteEffects.None, 0);
        }
    }
}
